(function() {
  "use strict";
  APP.Views.UserListView = Backbone.View.extend({
    events : {
    },
    initialize: function(params) {
      this.ajax = 2;
      var callbackGetUserList = {
        obj: this,
        name: 'getUserListSuccess'
      }
      APP.BeClient.getUserList(callbackGetUserList, undefined, {
        preloaderShow: false,
        preloaderHide: false
      });
      var callbackGetUserGroupList = {
        obj: this,
        name: 'getUserGroupListSuccess'
      }
      APP.BeClient.getUserGroupList(callbackGetUserGroupList, undefined, {
        preloaderShow: false,
        preloaderHide: false
      });
    },
    getUserListSuccess: function(data) {
      this.users = data.users;
      this.ajax--;
      if (this.ajax == 0) {
        this.renderRows()
      }
    },
    getUserGroupListSuccess: function(data) {
      this.groups = data.groups;
      this.ajax--;
      if (this.ajax == 0) {
        this.renderRows()
      }
    },
    renderRows: function() {
      var self = this;
      var tbody = this.$('#userListTable tbody');
      tbody.html('');
      var i = 0;
      this.rowViews = [];
      _.each(this.users, function(user) {
        var view = new APP.Views.UserListRowView({
          index: i+1,
          user: user,
          groups: self.groups
        });
        tbody.append(view.render().el);
        self.rowViews.push(view);
        i++;
      });
    },
    render: function(user) {
      this.$el.html(_.template($('#userListTemplate').html(), {}));
      return this;
    }
  });
}());